package com.zqj.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.common.annotation.PreventDuplicateSubmit;
import com.zqj.project.common.entity.R;
import com.zqj.project.common.request.PageParams;
import com.zqj.project.converter.OrderConverter;
import com.zqj.project.converter.OrderDetailConverter;
import com.zqj.project.dto.OrderDTO;
import com.zqj.project.service.OrderDetailService;
import com.zqj.project.service.OrderService;
import com.zqj.project.vo.OrderDetailVO;
import com.zqj.project.vo.OrderVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description: OrderController
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 19:38
 * @Version: 1.0
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/order")
@Tag(name = "Order API", description = "订单管理接口，支持增删改查以及分页功能")
@RequiredArgsConstructor
public class OrderController {

    // 自动生成依赖注入的构造函数
    private final OrderService orderService;
    private final OrderDetailService orderDetailService;

    // 分页查询订单列表
    @Operation(summary = "分页查询订单列表", description = "根据分页参数查询订单信息")
    @PostMapping("/page")
    public R<IPage<OrderVO>> queryOrderPage(@RequestBody @Validated @NotNull @Parameter(description = "分页参数及筛选条件") PageParams<OrderDTO> pageParams) {
        OrderDTO orderDTO = pageParams.getModel();
        IPage<OrderVO> orderVoPage = OrderConverter.toOrderVoPage(this.orderService.page(OrderConverter.toOrderModel(orderDTO), new Page<>(pageParams.getCurrent(), pageParams.getSize())));
        List<Long> orderIdList = orderVoPage.getRecords().stream().map(OrderVO::getId).toList();
        List<OrderDetailVO> orderDetailVoList = OrderDetailConverter.toOrderDetailVoList(this.orderDetailService.list(orderIdList));
        orderVoPage.getRecords().forEach(e -> e.setOrderDetailList(orderDetailVoList.stream().filter(detail -> detail.getOrderId().equals(e.getId())).toList()));
        return R.success(orderVoPage);
    }

    // 查询订单详情
    @Operation(summary = "查询订单详情", description = "根据订单ID查询具体订单的详细信息")
    @GetMapping("/info/{id}")
    public R<OrderVO> info(@PathVariable @Parameter(description = "订单ID", required = true) Long id) {
        OrderVO orderVo = OrderConverter.toOrderVo(this.orderService.info(id));
        orderVo.setOrderDetailList(OrderDetailConverter.toOrderDetailVoList(this.orderDetailService.list(id)));
        return R.success(orderVo);
    }

    // 订单下单
    @Operation(summary = "订单下单", description = "下单一条新的订单记录")
    @PostMapping("/pageOrder")
    @PreventDuplicateSubmit
    public R<String> pageOrder(@RequestBody @Validated @Parameter(description = "要下单的订单信息") OrderDTO orderDto) {
        return Boolean.TRUE.equals(this.orderService.pageOrder(OrderConverter.toOrderModel(orderDto))) ? R.success("订单下单成功") : R.fail("订单下单失败");
    }

    // 订单付款
    @Operation(summary = "订单付款", description = "根据订单ID更新订单付款信息")
    @PutMapping("/pay")
    @PreventDuplicateSubmit
    public R<String> pay(@RequestBody @Validated @Parameter(description = "要付款的订单信息，需包含ID") OrderDTO orderDto) {
        if (Objects.isNull(orderDto.getId())) {
            return R.fail("订单ID不能为空");
        }
        return Boolean.TRUE.equals(this.orderService.pay(OrderConverter.toOrderModel(orderDto))) ? R.success("订单付款成功") : R.fail("订单付款失败");
    }

    // 订单发货
    @Operation(summary = "订单发货", description = "根据订单ID更新订单发货信息")
    @PutMapping("/dispatch")
    @PreventDuplicateSubmit
    public R<String> dispatch(@RequestBody @Validated @Parameter(description = "要发货的订单信息，需包含ID") OrderDTO orderDto) {
        if (Objects.isNull(orderDto.getId())) {
            return R.fail("订单ID不能为空");
        }
        return Boolean.TRUE.equals(this.orderService.dispatch(OrderConverter.toOrderModel(orderDto))) ? R.success("订单发货成功") : R.fail("订单发货失败");
    }

    // 确认收货
    @Operation(summary = "确认收货", description = "根据订单ID更新订单完成信息")
    @PutMapping("/complete")
    @PreventDuplicateSubmit
    public R<String> complete(@RequestBody @Validated @Parameter(description = "要确认收货的订单信息，需包含ID") OrderDTO orderDto) {
        if (Objects.isNull(orderDto.getId())) {
            return R.fail("订单ID不能为空");
        }
        return Boolean.TRUE.equals(this.orderService.complete(OrderConverter.toOrderModel(orderDto))) ? R.success("订单确认收货成功") : R.fail("订单确认收货失败");
    }

    // 订单取消
    @Operation(summary = "订单取消", description = "根据订单ID更新订单取消信息")
    @PutMapping("/cancel")
    @PreventDuplicateSubmit
    public R<String> cancel(@RequestBody @Validated @Parameter(description = "要发货的取消信息，需包含ID") OrderDTO orderDto) {
        if (Objects.isNull(orderDto.getId())) {
            return R.fail("订单ID不能为空");
        }
        return Boolean.TRUE.equals(this.orderService.cancel(OrderConverter.toOrderModel(orderDto))) ? R.success("订单取消成功") : R.fail("订单取消失败");
    }

    // 修改订单
    @Operation(summary = "修改订单", description = "根据订单ID更新订单信息")
    @PutMapping("/edit")
    @PreventDuplicateSubmit
    public R<String> edit(@RequestBody @Validated @Parameter(description = "要修改的订单信息，需包含ID") OrderDTO orderDto) {
        if (Objects.isNull(orderDto.getId())) {
            return R.fail("订单ID不能为空");
        }
        return Boolean.TRUE.equals(this.orderService.edit(OrderConverter.toOrderModel(orderDto))) ? R.success("订单修改成功") : R.fail("订单修改失败");
    }

    // 删除订单
    @Operation(summary = "删除订单", description = "根据订单ID删除订单信息")
    @DeleteMapping("/delete/{id}")
    @PreventDuplicateSubmit
    public R<String> delete(@PathVariable @Parameter(description = "订单ID", required = true) Long id) {
        return Boolean.TRUE.equals(this.orderService.delete(id)) ? R.success("订单删除成功") : R.fail("订单删除失败");
    }

}